/*
 * AppConfig.h
 *
 *  Created on: 2015年5月22日
 *      Author: chuanjiang.zh@qq.com
 */

#ifndef VGS_APPCONFIG_H_
#define VGS_APPCONFIG_H_

#include <string>
#include <vector>
#include <map>
#include <memory>
#include "TStringCast.h"

namespace comn
{
    class IniFile;
}



class AppConfig
{
public:
    static AppConfig& instance();

	static const char SEPERATOR = '.';

public:
    AppConfig();
    virtual ~AppConfig();

    bool load(const char* filename);
    bool save(const char* filename);

    bool get(const std::string& path, std::string& value);

    std::string get(const std::string& path, const std::string& defValue=std::string());
    std::string get(const char* path, const char* defValue="");

    int getInt(const std::string& path, int defValue = 0);
    bool getBool(const std::string& path, bool defValue = false);
    double getDouble(const std::string& path, double defValue = 0.0);

    bool reload();

    template < class T >
    bool get(const std::string& path, T& t)
    {
        bool found = false;
        std::string value;
        if (get(path, value))
        {
            comn::StringCast::toValue(value, t);
            found = true;
        }
        return found;
    }

    std::string getFileName() const;

    void set(const std::string& path, const std::string& value);

    template < class T >
    void set(const std::string& path, const T& t)
    {
        std::string value = comn::StringCast::toString(t);
        set(path, value);
    }

	std::vector<std::string> getKeys(const std::string& path);

protected:
    std::string m_filename;
    std::auto_ptr< comn::IniFile >    m_iniFile;

};

#endif /* VGS_APPCONFIG_H_ */
